package com.xy.xylive.hstapi;

import java.util.HashMap;
import java.util.Map;
import cn.hutool.jwt.JWT;
import com.xy.xylive.config.HstConf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 好视通api接口认证工具类
 */
@Component
public class SignUtil {
    @Autowired
    private HstConf hstConf;

    public String quickGenerateJWT() {
        Map<String, Object> headerMap = new HashMap<>();
        headerMap.put("alg", "HS256");
        headerMap.put("typ", "JWT");

        Map<String, Object> payloadMap = new HashMap<>();
        payloadMap.put("appKey", hstConf.getAppKey());
        payloadMap.put("timestamp", System.currentTimeMillis() / 1000);

        String jwt = JWT.create().addHeaders(headerMap).addPayloads(payloadMap)
                .setSigner("HS256", hstConf.getAppSecret().getBytes()).sign();

        return jwt;
    }

    public String getAuthorization() {
        return "Bearer " + quickGenerateJWT();
    }

    public Map<String, String> getAuthorizationHeader() {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Authorization", getAuthorization());
        headerMap.put("Accept-Language", "zh-CN");
        return headerMap;
    }

}
